#!/usr/bin/env ruby

# $Id$

require 'genus/pdgen'
require 'example/template'
require 'optparse'

GENUS_VERSION = '0.3.1'
DESCRIPTOR = 'Descriptor'

file = nil
ARGV.options do |opts|
  opts.banner = "Usage: gen [#{DESCRIPTOR} | options] ... see README for more info"
  opts.on('-t', '--template-descriptor', 'Generate simple template to get you started') do
    File::open(DESCRIPTOR, 'w') { |f| f << TEMPLATE }
    puts "Descriptor file generated. Run 'gen' to make persistence.xml"
    exit
  end
  opts.on('-d', '--descriptor=FILE', 'Descriptor file') { |val| file = val }
  opts.on('-v', '--version', 'Print version')          { puts "Genus version: #{GENUS_VERSION}"; exit}
  opts.on('-h', '--help', 'Show this help message.')    { puts opts; exit }
  
  opts.parse!
end

file = case
  when ARGV.size == 0 && File::exists?(DESCRIPTOR) && file.nil? then DESCRIPTOR
  when ARGV.size == 1 then ARGV.first
end if file.nil?

if file.nil? || !File::exist?(file)
  puts "Descriptor #{file + ' ' unless file.nil?}does not exist. See README for more info."
  exit
end

PersistenceDescriptorGenerator.new(file).exec